package dao

import (
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"my_music_server/config"
)

var (
	DB *gorm.DB
)

func InitMySQL() (err error) {
	//dst:="root:root@(127.0.0.1:3306)/my_music?charset=utf8mb4&parseTime=True&loc=Local"
	//dst := config.ServerAllConfig.DatasourceUrl
	dst := config.AppicationConfig.DataSource.Url
	DB, err = gorm.Open("mysql", dst)
	//if config.ServerAllConfig.IsDatasourceDebug {
	//	DB = DB.Debug()
	//}
	if config.AppicationConfig.DataSource.Debug {
		DB = DB.Debug()
	}

	//自动迁移模型到数据库
	//TODO 配置文件待优化
	//DB.AutoMigrate()

	if err != nil {
		return
	}
	return DB.DB().Ping()
}

func Close() {
	DB.Close()
}
